package org.sbbs.demo.webapp.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.sbbs.base.webapp.action.BaseDwzAction;

public class UploadifyDemoAction extends BaseDwzAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String Filename;

	public void setFilename(String filename) {
		Filename = filename;
	}

	private File imageFile;

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}

	public String uploadImageFile() {

		HttpServletRequest req = this.getRequest();
		// String fileName =(new Date()).getTime()+"";
		String fileName = this.Filename;
		if (fileName != null && !fileName.equals("")) {
			fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
		} else {
			fileName = (new Date()).getTime() + ".jpg";
		}
		String serverPath = "//uploadFiles//";
		serverPath = this.getRequest().getRealPath(serverPath);
		OptFile.mkParent(serverPath + "\\" + fileName);
		File targetFile = new File(serverPath + "\\" + fileName);
		try {
			OptFile.copyFile(imageFile, targetFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return this.ajaxReturn.success(fileName);
	}

	public String deleteImageFile() {
		String serverPath = "//uploadFiles//";
		serverPath = this.getRequest().getRealPath(serverPath);
		String filePath = serverPath + "\\" + this.Filename;
		File img = new File(filePath);
		boolean res = img.delete();
		return this.ajaxReturn.success(this.SUCCESS);
	}
}
